<template>
  <div>
    <div class="logo">
      <img src="@/assets/milogo.png" alt="">
      <h4>小米账号登录</h4>
    </div>
    <div class="login_acount">
      <input
        type="text"
        placeholder="邮箱/手机号码/小米ID"
        class="acount"
        v-model="username"
      >
      <input
        type="password"
        placeholder="密码"
        class="pwd"
        v-model="password"
      >
      <button class="btn btn_login" @click="login">登录</button>
      <button class="btn btn_change">手机短信登录注册</button>
    </div>
    <fieldset class="oth_type_tit">
      <legend align="center" class="oth_type_txt">其他方式登录</legend>
    </fieldset>
    <div class="links">
      <a href="https://account.xiaomi.com/pass/sns/login/auth?appid=2996826273&&callback=https%3A%2F%2Fshopapi.io.mi.com%2Fapp%2Fshop%2Fauth%3Flogid%3D1543046790.81361%26sign%3De5990dac32185f233934d4bf4a757a7e%26followup%3Dhttps%253A%252F%252Fm.youpin.mi.com%252Fucenter&sid=miotstore"
      class="blog">
        <i></i>
      </a>
      <a href="https://account.xiaomi.com/pass/sns/login/auth?appid=wxxmzh&scope=snsapi_login&callback=https%3A%2F%2Fshopapi.io.mi.com%2Fapp%2Fshop%2Fauth%3Flogid%3D1543046790.81361%26sign%3De5990dac32185f233934d4bf4a757a7e%26followup%3Dhttps%253A%252F%252Fm.youpin.mi.com%252Fucenter&sid=miotstore"
      class="alipay">
        <i></i>
      </a>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import { Toast } from 'mint-ui'

export default {
  name: 'Login',
  data () {
    return {
      username: '',
      password: ''
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {
    user (val) {
      if (val.isLogin === true) {
        const { redirect = '/home' } = this.$route.params
        this.$router.push(redirect)
      }
    }
  },
  methods: {
    ...mapMutations(['loginSuccess']),
    login () {
      const reg = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/
      const pattern = /^[\w_-]{6,16}$/
      let name = reg.test(this.username)
      let pwd = pattern.test(this.password)
      if (name === true && pwd === true) {
        this.$http('/api/user')
          .then((resp) => {
            if (resp.data.res_code === 200) {
              this.loginSuccess(resp.data.data)
              Toast('登陆成功')
            } else {
              alert('输入信息有误')
              location.reload()
            }
          })
      } else {
        Toast('输入的邮箱或是密码格式有误')
      }
    }
  }
}
</script>

<style lang="scss" scoped>
  .logo {
    width: 319px;
    height: 81px;
    padding: 30px 0 10px 0;
    margin: auto;
    img {
      display: block;
      margin: 0 auto;
      margin-bottom: 15px;
    }
    h4 {
      height: 18px;
      line-height: 18px;
      font-size: 14px;
      text-align: center;
    }
  }
  .login_acount {
    width: 319px;
    margin: 0 auto;
    text-align: center;
    input:focus {outline: none;}
    input {
      width: 319px;
      height: 15px;
      padding: 12px 0;
      background-color: #fff !important;
      border: none;
      border-bottom: 1px solid #dedede;
    }
    .btn {
      margin-top: 24px;
      width: 319px;
      height: 33px;
      border: 1px solid;
      border-radius: 6px;
      font-size: 13px;
    }
    .btn_login {
      background-color: #ff6700;
      border-color: #ff6700;
      color: #fff;
    }
    .btn_change {
      background-color: #fff;
      border-color: #dedede;
      color: #000;
    }
  }
  .oth_type_tit {
    width: 319px;
    height: 15px;
    padding-top: 10px;
    margin: 0 auto;
    border-top: 1px solid #f2f2f2;
    margin-top: 40px;
    font-size: 12px;
    .oth_type_txt {
      color: #b0b0b0;
      width: 80px;
    }
  }
  .links {
    margin: 0 auto;
    padding-top: 10px;
    text-align: center;
    width: 319px;
    a {
      display: inline-block;
      width: 30px;
      height: 30px;
      border-radius: 50%;
      text-align: center;
    }
    .blog {
      background-color: #ed9090;
      margin-right: 15px;
      i {
      display: inline-block;
      margin-top: 6px;
      width: 18px;
      height: 18px;
      background: #ed9090 url(../assets/icons_type.png) no-repeat;
      background-position: -38px 0;
      }
    }
    .alipay {
      background-color: #6bb6ea;
      i {
        display: inline-block;
        width: 18px;
        height: 18px;
        margin-top: 6px;
        background: #6bb6ea url(../assets/icons_type.png) no-repeat;
        background-position: -57px 0;
      }
    }
  }
</style>
